﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimaryAlgorithm.CountAndSay
{
    /// <summary>
    /// 外观数列
    /// </summary>
    class MainProject
    {
        static void Main(string[] args)
        {
            Console.WriteLine(CountAndSay(9));
            Console.ReadKey();
        }
        public static string CountAndSay(int n)
        {
            if (n == 1) return "1";
            n--;
            return Count(n);
        }

        public static string Count(int n)
        {
            string num = string.Empty;
            num = n == 1 ? "1" : Count(n - 1);

            string str = string.Empty;
            int count = 1;
            int flagNum = int.Parse(num[0].ToString());
            int size = 1;

            while (size < num.Length)
            {
                int curNum = int.Parse(num[size].ToString());
                if (flagNum == curNum)
                {
                    count++;
                }
                else
                {
                    str += count;
                    str += flagNum;
                    flagNum = curNum;
                    count = 1;
                }
                size++;
            }
            if (size == num.Length)
            {
                str += count;
                str += flagNum;
            }
            return str;
        }
    }
}
